package com.controllor;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.dao.ManagerDao;
import com.dao.OrdinaryEmployeesDao;
import com.entity.User;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.util.JSON_to_layui;

/**
 * Servlet implementation class ManagerServlet
 */
@WebServlet("/ManagerServlet")
public class ManagerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    public ManagerServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		User USER = (User) session.getAttribute("user");
		String userid = USER.getUserid();
//		String userid = (String)session.getAttribute("userid");
//*****“lqq”临时测试，记得改为“userid”*********
//		String userid = "test";
		session.setAttribute("userid", userid);//临时用，本该在登录servlet进行session。setAttribute（）
		String button = request.getParameter("button");
//		String button ="1";
		
		//获取同部门所有职工的基本信息 或薪资信息
		if("1".equals(button)) {
			System.out.println("Manager..button"+button);
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = (Integer.parseInt(request.getParameter("page"))-1) * limit;
			String conditionValue = request.getParameter("conditionValue");
			String condition = request.getParameter("condition");
			
			System.out.println("conditionValue"+conditionValue);
			ObjectMapper mapper = new ObjectMapper();
			mapper.enable(SerializationFeature.INDENT_OUTPUT);	//序列化
			String json = mapper.writeValueAsString(ManagerDao.getUserInfo(userid,condition,conditionValue,page,limit));
			if(json != null || json.length() != 0) {
				json = JSON_to_layui.getLayuiData(json,0,ManagerDao.count,"");
			}
			response.getWriter().print(json);
		}
		//经理录入请假旷工
		else if("2".equals(button)) {
			System.out.println("Manager..button"+button);
			PrintWriter out = response.getWriter();
			String operater = request.getParameter("operater");
			System.out.println("operater="+operater);
			int leavecount = Integer.parseInt(request.getParameter("leavecount"));
			int absentcount = Integer.parseInt(request.getParameter("absentcount"));
			String applydescribe = request.getParameter("applydescribe");
			if(ManagerDao.createApply(userid, operater, leavecount, absentcount, applydescribe,"1")) {
				out.print("请假旷工情况已提交，请耐心等待审核!");
			}else {
				out.print("请假旷工情况提交失败，请稍后再试！");
			}
		}
		//经理申请奖金福利
		else if("3".equals(button)) {
			System.out.println("Manager..button"+button);
			PrintWriter out = response.getWriter();
			String operater = request.getParameter("operater");
			System.out.println("operater="+operater);
			float festivalbouns = Integer.parseInt(request.getParameter("festivalbouns"));
			float yearbonus = Integer.parseInt(request.getParameter("yearbonus"));
			String applydescribe = request.getParameter("applydescribe");
			if(ManagerDao.createApply(userid, operater, festivalbouns, yearbonus, applydescribe,"2")) {
				out.print("奖金申领已提交，请耐心等待审核!");
			}else {
				out.print("奖金申领提交失败，请稍后再试！");
			}
		}
		//经理录入销售提成
		else if("4".equals(button)) {
			System.out.println("Manager..button"+button);
			PrintWriter out = response.getWriter();
			String operater = request.getParameter("operater");
			System.out.println("operater="+operater);
			float commission = Float.parseFloat(request.getParameter("commission"));
			String applydescribe = request.getParameter("applydescribe");
			if(ManagerDao.createApply(userid, operater, commission,null, applydescribe,"3")) {
				out.print("提成情况已提交，请耐心等待审核!");
			}else {
				out.print("提成提交失败，请稍后再试！");
			}
		}
		//显示部门申请
		else if("5".equals(button)) {
			System.out.println("Manager..button"+button);
			int limit = Integer.parseInt(request.getParameter("limit"));
			int page = (Integer.parseInt(request.getParameter("page"))-1) * limit;
			String status = request.getParameter("condition");
			System.out.println("status:"+status);
			
			ObjectMapper mapper = new ObjectMapper();
			mapper.enable(SerializationFeature.INDENT_OUTPUT);	//序列化
			String json = mapper.writeValueAsString(ManagerDao.getDeptApply(userid,status, page, limit));
			if(json != null || json.length() != 0) {
				json = JSON_to_layui.getLayuiData(json,0,ManagerDao.count,"");
			}
			response.getWriter().print(json);
		}
		//处理部门申请
		else if("6".equals(button)) {
			System.out.println("Manager..button"+button);
			PrintWriter out = response.getWriter();
//			boolean f = false;
			String status = request.getParameter("status");
			String operater = request.getParameter("operater");
			int applyid = Integer.parseInt(request.getParameter("applyid"));
			int overtime = Integer.parseInt(request.getParameter("overtime"));
			int weekovertime = Integer.parseInt(request.getParameter("weekovertime"));
			int festovertime = Integer.parseInt(request.getParameter("festovertime"));
			System.out.println("参数："+status+"  "+applyid+"  "+operater+"  "+overtime+"  "+weekovertime+"  "+festovertime);
			if("1".equals(status)) {
				if(ManagerDao.doDeptApply(status,userid, operater, applyid,overtime,weekovertime,festovertime)) {
					out.println("同意成功！");
				}else {
					out.println("同意失败！");
				}
			}else {
				if(ManagerDao.doDeptApply(status,userid, operater, applyid,overtime,weekovertime,festovertime)) {
					out.println("拒绝成功！");
				}else {
					out.println("拒绝失败！");
				}
			}
			
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
}
